home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / PacMan / GameBrain.m < prev    next >
Encoding:
Text File  |  1992-07-24  |  8.1 KB  |  320 lines

  1. /* Generated by Interface Builder */
  2.  
  3. #import "GameBrain.h"
  4. #import "PreferencesBrain.h"
  5. #import "PlayerUpView.h"
  6. #import "InfoController.h"
  7. #import "FruitView.h"
  8. #import "WinDel.h"
  9. #import <stdio.h>
  10. #import <string.h>
  11.  
  12. static int ghostScores[4] = { 200, 400, 800, 1600 };
  13.  
  14.  
  15. @implementation GameBrain
  16.  
  17. - init        // designated initializer sets up game variables
  18. {        // to sensible values.
  19.     score = 0;
  20.     level = 0;
  21.     paused = NO;
  22.     ranOnce = NO;
  23.     
  24.     return self;
  25. }
  26.  
  27. /* methods to get at important variables */
  28. - (int)startLevel { return [preferencesBrain startLevel]; }
  29. - (int)level      { return level;      }
  30. - (int)speed      { return speed;      }
  31. - (int)paused     { return paused;     }
  32.  
  33. - showHigh
  34. {            // update the "high score field    
  35.     [topScoreText setIntValue:[highScoreTable highScore:0]];
  36.     return self;
  37. }
  38.  
  39. - addToScore:(int)increment    // update the player's score
  40. {
  41.     score += increment;
  42.     if (score - lastBonus >= pointsToNextBonus) {
  43.         [pacsLeft incPacs];
  44.         lastBonus += pointsToNextBonus;
  45.         // assures bonus at 10000, 35000, 55000, 75000, and every 25000 after
  46.         if ((pointsToNextBonus < 20000) || ((pointsToNextBonus == 20000) &&
  47.             (lastBonus >= 75000)))
  48.                 pointsToNextBonus += 5000;
  49.     }
  50.     [scoreText setIntValue:score];
  51.     if ((score > [highScoreTable highScore:0]) && ![gameScreen demoMode]) {
  52.         [topScoreText setIntValue:score];
  53.     }
  54.     return self;
  55. }
  56.  
  57. - zeroScore
  58. {
  59.     score = 0;            // clear score
  60.     [self addToScore:0];    // force display
  61.     return self;
  62. }
  63.  
  64. - gameOver:sender        // end the game, take high scores, etc.
  65. {                // calls gameOver method (this is IB wrapper)
  66.     [self gameOver];
  67.     return self;
  68. }
  69.  
  70. - pauseGame:sender        // toggle pause status of the game
  71. {
  72.     const char *title = [pauseMenuCell title];
  73.     
  74.     if (!strcmp(title,"Pause")) {  // returns zero if equal
  75.         [self pause];
  76.     } else {
  77.         [self unpause];
  78.     } 
  79.     return self;
  80. }
  81.  
  82. - (int)pause                // pause game  
  83. {
  84.     if ([gameScreen gameState]==GAMEOVER) return NO; // no pausing if over
  85.     [pauseMenuCell setTitle:"Unpause"];
  86.     [gameScreen pause:self];
  87.     [gameWindow setTitle:"PacMan - Paused"];
  88.     paused = YES;
  89.     return YES;
  90. }
  91.  
  92. - unpause            // unpause game  
  93. {
  94.     if ([gameScreen gameState]==GAMEOVER) {
  95.         [pauseMenuCell setEnabled:NO];
  96.         return self; // no pausing if over
  97.     }
  98.     [pauseMenuCell setTitle:"Pause"];
  99.     [gameScreen unpause:self];
  100.     [gameWindow setTitle:"PacMan"];
  101.     paused = NO;
  102.     return self;
  103. }
  104.  
  105. - startNewGame:sender        // starts a new game
  106. {    
  107.     if (([preferencesBrain alert]) && ([gameScreen gameState]!=GAMEOVER)) {
  108.         if ([highScoreTable highScore:9] < score) {
  109.             if (NXRunAlertPanel("You've got a high score!",
  110.                 "Do you want to throw away the current game?",
  111.             "You betcha!", "Um, no.", NULL) != NX_ALERTDEFAULT) {
  112.         return self;  // allow "graceful escape"
  113.         }
  114.     } else {
  115.             if (NXRunAlertPanel(NULL,
  116.                 "Do you want to throw away the current game?",
  117.                 "You betcha!", "Um, no.", NULL) != NX_ALERTDEFAULT) {
  118.             return self;  // allow "graceful escape"
  119.             }
  120.         }
  121.     }
  122.     [topScoreText setIntValue:[highScoreTable highScore:0]];
  123.     score = 0;
  124.     level = 0;
  125.     lastBonus = 0;
  126.     pointsToNextBonus = 10000;    // first bonus at 10000 points
  127.     ranOnce = YES;
  128.     [pauseMenuCell setEnabled:YES];
  129.     [scoreText setIntValue:score];
  130.     [self nextLevel:self];   // by the nextLevel: method.
  131.     if (paused) [self unpause];
  132.     [gameWindow makeKeyAndOrderFront:self];
  133.     [pacsLeft setNumUp:3];    // start with 3 pacs
  134.     [gameScreen restartGame];
  135.     return self;
  136. }
  137.  
  138. - unpauseGame:sender        // unpause the game 
  139. {
  140.     return [self pauseGame:sender]; // handles toggle of menuCell
  141. }
  142.  
  143. - nextLevel:sender        // move to next level-- called when all viruses
  144.                 // are gone or to start game
  145. {
  146.     level++;
  147.     [levelText setIntValue:level];
  148.     [fruitBasket showLevel:level];
  149.     [gameScreen setUpScreen];
  150.     return self;
  151. }
  152.  
  153. - gameOver            // tidy up game, allow high score name entry
  154.                 
  155. {
  156.     // remove demo mode title...
  157.     if ([gameScreen demoMode]) [gameWindow setTitle:"PacMan"];
  158.     // if not demo, it's possible to get a high score entry...
  159.     else [highScoreTable putInHighScores:score];
  160.     [pauseMenuCell setEnabled:NO];
  161.     return self;
  162. }
  163.  
  164.  
  165. - (int)ateGhost
  166. {
  167.     int temp = ghostScores[ghostCount];
  168.     
  169.     [self addToScore:temp];
  170.     ghostCount++; ghostCount &= 0x3;
  171.     return temp;
  172. }
  173.  
  174. - resetGhostScore
  175. {
  176.     ghostCount = 0;
  177.     return self;
  178. }
  179.  
  180.  
  181.  
  182. /****
  183. *****  Application DELEGATE methods.  Special things to do on startup, unhide,
  184. *****  hide, and so on.
  185. ****/
  186.  
  187. - appWillInit:sender        // after init, but before 1st event.
  188. {
  189.     alert = NXGetAlertPanel("Pac Man",
  190.         "Just a moment while I load the images...", NULL, NULL, NULL);
  191.     [alert makeKeyAndOrderFront:self];
  192.     return self;
  193. }
  194.  
  195. - appDidInit:sender        // after init, but before 1st event.
  196. {
  197.     [pauseMenuCell setEnabled:NO];
  198.     
  199.     // methods to load the stuff that takes a while.
  200.     [loadingPanel makeKeyAndOrderFront:self];
  201.     [gameScreen loadPix];            // images, background
  202.     [loadingPanel orderOut:self];
  203.  
  204.     [preferencesBrain readDefaults:self];
  205.     [gameScreen getPreferences];    // set up final init.
  206.     [gameScreen registerWindow];    // set up dragging.
  207.     [[gameScreen animate:self] update];        // start up animation
  208.     [alert orderOut:self];
  209.     NXFreeAlertPanel(alert);
  210.     [topScoreText setIntValue:[highScoreTable highScore:0]];
  211.     [levelText setIntValue:1];
  212.     [scoreText setIntValue:0];
  213.     [fruitBasket showLevel:1];
  214.     [[fruitBasket window] orderFront:self];
  215.     [[scoreText window] orderFront:self];    // get the stats window visible
  216.     [gameWindow makeKeyAndOrderFront:self]; // activate game window
  217.     [gameWindow makeFirstResponder:gameScreen]; // set up first responder
  218.     if ([preferencesBrain firstTimeCheck]) [infoController readme:self];
  219.     else if ([preferencesBrain autoStart]) [self startNewGame:self];
  220.     return self;
  221. }
  222.  
  223. - appDidBecomeActive:sender
  224. {
  225.     [gameWindow makeKeyAndOrderFront:self];
  226.     [gameWindow makeFirstResponder:gameScreen];
  227.     if ([preferencesBrain autoUnPause]) [self unpause];
  228.  
  229.     // make sure the windows are layered properly
  230.     if ([[[fruitBasket window] delegate] windowUp])
  231.         [[fruitBasket window] orderFront:self];
  232.     if ([[[scoreText window] delegate] windowUp])
  233.         [[scoreText window] orderFront:self];
  234.     [gameWindow orderFront:self];
  235.  
  236.     return self;
  237. }
  238.  
  239. - appDidHide:sender
  240. {
  241.     [self pause];
  242.     return self;    // pause game on Command-h
  243. }
  244.  
  245. - appDidResignActive:sender
  246. {
  247.     if ([gameScreen demoMode]) return self;
  248.     [self pause];
  249.     return self;    // pause game on app deactivate
  250. }
  251.  
  252. - appDidUnhide:sender
  253. {
  254.     [gameWindow makeKeyAndOrderFront:self];
  255.     [gameWindow makeFirstResponder:gameScreen];
  256.    if ([preferencesBrain autoUnPause]) [self unpause];
  257.     // make sure the windows are layered properly
  258.     if ([[[fruitBasket window] delegate] windowUp])
  259.         [[fruitBasket window] orderFront:self];
  260.     if ([[[scoreText window] delegate] windowUp])
  261.         [[scoreText window] orderFront:self];
  262.     [gameWindow orderFront:self];
  263.     return self;
  264. }
  265.  
  266. - appWillTerminate:sender        // update DEFAULTS here 
  267. {
  268.     [preferencesBrain writeDefaults:self];
  269.     [highScoreTable writeHighScores];
  270.     return self;
  271. }                    
  272.  
  273. - quit:sender
  274. {
  275.     if (([gameScreen gameState] != GAMEOVER) && (![gameScreen demoMode]) &&
  276.             ([preferencesBrain alert])) {
  277.         // Verify that player wants to leave game
  278.             [self pause];
  279.         if (!NXRunAlertPanel(NULL,
  280.                 "There's a game in progress... Do you really want to quit?",
  281.                 "Absolutely.", "No way!", NULL)) {
  282.             return [self unpause];
  283.     }    }
  284.     return [NXApp terminate:sender];
  285. }
  286.  
  287. - windowDidResginMain:sender    // do pause if window loses main status
  288. {
  289.     [self pause];
  290.     return self;    // pause game
  291. }
  292.  
  293. - windowDidResignKey:sender        // do pause if window loses key status
  294. {
  295.     [self pause];
  296.     return self;    // pause game
  297. }
  298.  
  299. - windowDidBecomeKey:sender        // do unpause if window gains key status 
  300. {
  301.     [gameWindow makeFirstResponder:gameScreen];
  302.     if ([preferencesBrain autoUnPause]) [self unpause];    // unpause on unhide*/
  303.     return self;
  304. }
  305.  
  306. - windowDidMove:sender        // move fruit basket and status with game window 
  307. {
  308.     NXRect gameFrame;
  309.     
  310.     [gameWindow getFrame:&gameFrame];
  311.     [[fruitBasket window] moveTo:(NX_X(&gameFrame) - 137)
  312.         :(NX_Y(&gameFrame) - 37)];
  313.     [[scoreText window] moveTo:(NX_X(&gameFrame) - 137)
  314.         :NX_Y(&gameFrame)];
  315.     return self;
  316. }
  317.  
  318.  
  319. @end
  320.